package com.yuyu.qingmangmall.product.app;

import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.R;
import com.yuyu.qingmangmall.product.entity.ProductAttrValueEntity;
import com.yuyu.qingmangmall.product.service.AttrService;
import com.yuyu.qingmangmall.product.service.ProductAttrValueService;
import com.yuyu.qingmangmall.product.vo.AttrRespVo;
import com.yuyu.qingmangmall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 商品属性
 *
 * @author yuwengsheng
 * @email 3337002363@qq.com
 * @date 2021-05-31 01:02:11
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    //  /product/attr/base/list/{catelogId}
    //  /product/attr/sale/list/{catelogId}
    @GetMapping("/{attrType}/list/{catelogId}")
    public R attrBaseList(@RequestParam Map<String,Object> params, @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.queryBasePage(params,catelogId,attrType);

        return R.ok().put("page",page);
    }

    // /product/attr/info/{attrId}
    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     *  获取spu规格
     *  /product/attr/base/listforspu/{spuId}
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSup(@PathVariable("spuId") Long supId) {
        List<ProductAttrValueEntity> productAttrList = productAttrValueService.getAttrBySpuId(supId);
        return R.ok().put("data",productAttrList);
    }

    /**
     *  修改商品规格
     *  /product/attr/update/{spuId}
     * @param spuId
     * @param attrValues
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> attrValues) {
        productAttrValueService.updateBatchBySpuId(spuId,attrValues);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);

        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }


}
